package com.example.uchain.mapper;

import com.example.uchain.demo.Author;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AuthorMapper {

    //查询
    @Select("select * from author")
    public List<Author> findAll();

    @Select("select * from author where member_id =#{memberId}")
    Author findById(@Param("memberId") Long memberId);

    //添加
    @Insert("insert into author (name,id_type,id_number,phone_number,mail,id_start_time,id_end_time,update_time,if_longterm)" +
            " values(#{name},#{idType},#{idNumber},#{phoneNumber},#{mail},#{idStartTime},#{idEndTime},#{updateTime},#{ifLongterm})")
    @Options(useGeneratedKeys = true, keyProperty = "memberId")
    int AddAuthor(Author author) ;


    @Delete("delete from author where member_id = #{memberId}")
    //批量删除
    void deleteAuthorById(Long memberId);

    //根据id然后修改商品信息
    public void updateById(Author author) ;

    List<Author> findAllWithCode(
            @Param("name") String name,
            @Param("memberId") Long memberId,
            @Param("phoneNumber") String phoneNumber,
            @Param("code") Integer code);


    @Results(id = "AuthorTotalByDayResultMap", value = {
            @Result(column = "date", property = "date"),
            @Result(column = "total_author_count", property = "count")
    })
    @Select("SELECT DATE(update_time) AS date, COUNT(*) AS total_author_count " +
            "FROM author " +
            "WHERE update_time >= DATE_SUB(CURDATE(), INTERVAL 9 DAY) " +
            "GROUP BY DATE(update_time) " +
            "ORDER BY DATE(update_time)")
    List<Map<String, Object>> totalAuthorCountByDay();


    @Results(id = "AuthorCountByDayResultMap", value = {
            @Result(column = "date", property = "date"),
            @Result(column = "new_author_count", property = "count")
    })
    @Select("SELECT DATE(update_time) AS date, COUNT(*) AS new_author_count " +
            "FROM author " +
            "WHERE update_time >= DATE_SUB(CURDATE(), INTERVAL 9 DAY) " +
            "GROUP BY DATE(update_time) " +
            "ORDER BY DATE(update_time)")
    List<Map<String, Object>> newAuthorCountByDay();
}
